/**
 * Created by zy on 2017/3/28.
 */

$(document).ready(function () {
    // document.body.addEventListener('touchmove', function (evt) {
    //     if (!evt._isScroller[$(evt.target).attr('id')]) {
    //         evt.preventDefault();
    //     }
    // });

    // 禁止上拉、下拉
    function overscroll(el) {
        var startY, endY;
        el.addEventListener('touchstart', function (evt) {
            startY = evt.touches[0].clientY;
        });
        el.addEventListener('touchmove', function (evt) {
            var top = el.scrollTop,
                currentScroll = top + el.offsetHeight,
                id = $(el).attr('id');
            endY = evt.touches[0].clientY;
            if (!evt._isScroller) {
                evt._isScroller = {};
            }

            if (top < 3 && endY > startY) {    // 顶部下拉禁止
                evt._isScroller[id] = false;
            } else if (el.scrollHeight - currentScroll < 1 && endY < startY) {   // 底部上拉禁止
                evt._isScroller[id] = false;
            } else if (el.offsetHeight < el.scrollHeight) {
                evt._isScroller[id] = true;
            } else {
                evt._isScroller[id] = false;
            }
        });
    }

    $.each(document.querySelectorAll('.scroll'), function () {
        overscroll($(this)[0]);
    });
});